﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Store_Keeper_SK_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["index"] = 0;
        }
    }
    protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = int.Parse(ViewState["index"].ToString());
        setAlldata(index);
    }
    public void setAlldata(int index)
    {
        string proid = DropDownList4.Text;

        Bestlife.ERP.Entities.product pro = new Bestlife.ERP.Entities.product();
        pro = Bestlife.ERP.BusinessRules.B_PurchaseOrder.setalldata(proid);
        subcatname.Text = pro.Sub_Cat_Name1;
        catname.Text = pro.Cat_Name1;
        brand.Text = pro.Brand;
        Productid.Text = pro.Pro_ID;


    }
    protected void retailprice_TextChanged(object sender, EventArgs e)
    {
        if (retailprice.Text != "0.00")
        {
            string user = Session["user"].ToString();
            string proid = DropDownList4.Text;
            decimal markprice = (decimal)(double.Parse(retailprice.Text));
            decimal cost = (decimal)(double.Parse(purchaseprice.Text));
            Bestlife.ERP.Entities.product pro = new Bestlife.ERP.Entities.product();
            productprice.Text = Bestlife.ERP.BusinessRules.productBV_PV.setdisprice(cost, markprice).ToString();
            decimal price =Convert.ToDecimal(productprice.Text);
            pointvalue.Text = Bestlife.ERP.BusinessRules.productBV_PV.setPV(cost,price).ToString();
            businesspoint.Text = Bestlife.ERP.BusinessRules.productBV_PV.setBV(cost, price).ToString();
            Bestlife.ERP.BusinessRules.productBV_PV.insertpvbv(cost, markprice, proid, user);
            
             

        }
    }
    protected void ASPxButton2_Click(object sender, EventArgs e)
    {
        string user = Session["username"].ToString();
        try
        {
            string[] imageArr = new string[4];
            Bestlife.ERP.Entities.product pro = new Bestlife.ERP.Entities.product();
            
            pro.Cat_ID = DropDownList4.Text;
            pro.Point_Value = (decimal)(double.Parse(pointvalue.Text));
            
            pro.Pro_Code = ASPxComboBox1.Text;
            pro.Pro_ID = Productid.Text;
            pro.Sub_Cat_Name1=subcatname.Text;
            pro.Pro_Price = (decimal)(double.Parse(productprice.Text));
        
            pro.Pur_Price = (decimal)(double.Parse(purchaseprice.Text));
            pro.Ret_Price = (decimal)(double.Parse(retailprice.Text));
            
           

            Bestlife.ERP.BusinessRules.productBV_PV.insert(pro,user);

            //Images Upload
            // Get the HttpFileCollection
            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    hpf.SaveAs(MapPath("~/C:/inetpub/vhosts/bestlifecrown.com/httpsdocs/web/img/productimage") + "\\" +
                      System.IO.Path.GetFileName(hpf.FileName));
                    string image = hpf.FileName;
                    imageArr[i] = image;

                }
            }
            string pid = Productid.Text;
            Bestlife.ERP.BusinessRules.productBV_PV.insertimage(imageArr, pid);
            
        }
        catch
        {
            
            Label5.Text = "This product is alredy entered to system";
        }
    }
    }
